package com.lyl.momanapp.frament;

import android.content.Intent;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.Upimg;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyFragment;
import com.lyl.momanapp.xin.DragView;
import com.lyl.momanapp.xin.YeActivity;

import static com.lyl.momanapp.bean.URL.sejiandshopURL;

/**
 * Created by lyl on 2017/10/17.
 */

public class OneThree1 extends MyFragment {
    private RelativeLayout mRL;
    private ImageView imageView;
    Button genghuan;
    Button huanyuan;
    private DragView mDragView;
    View dialogView;
    ImageView tupian;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 111111111: {
                    Upimg upimg = new Gson().fromJson(msg.obj.toString(), Upimg.class);

                }
                break;
            }
        }
    };

    @Override
    public void onClick(View v) {

    }

    @Override
    public int inflaterRootView() {
        return R.layout.frament_one_three1;
    }

    @Override
    public void initUI() {
        mRL = findView(R.id.one_three1_rl);
        imageView = findView(R.id.yifuback);
        genghuan = findView(R.id.genghuan);
        huanyuan = findView(R.id.huanyuan);
        dialogView = LayoutInflater.from(getActivity())
                .inflate(R.layout.my_self_view, null);
        tupian = dialogView.findViewById(R.id.tupian);
        mDragView = (DragView) findView(R.id.dragview);
        mDragView.addDragView(dialogView, 0, 400, 380, 760, false, true);

        //   tupian.setOnTouchListener(new ImageTouchListener());
        genghuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), YeActivity.class));
            }
        });
        huanyuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(getActivity()).load(URL.sejiURL).diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                        .skipMemoryCache(true).into(tupian);
            }
        });


    }

    public void baocun() {
        String image = new MyUtils().saveImage(mRL, new MyUtils().getTime(), getContext());
        sejiandshopURL = image;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
        } else {
            Glide.with(getActivity())
                    .load(URL.productTemplates.getOriginal_image().get(0))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                    .skipMemoryCache(true)
                    .into(imageView);
            Glide.with(getActivity()).load(URL.sejiURL)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                    .skipMemoryCache(true)
                    .into(tupian);
        }
    }

    @Override
    public void initData() {
//        if (URL.Userlogin==null){
//            startActivity(new Intent(getContext(), UserLoginActivity.class));
//        }else {
//            udimg1();
//        }

    }

    @Override
    public void addListeners() {

    }

    private void udimg1() {
        Log.i("111111111111111111", "udimg: " + sejiandshopURL);
        new MyUtils().xUutlisImg(URL.URL_img_up, URL.sejiURL, handler, 111111111);
    }


    public void GengHuan() {
        Log.i("asdasd", "asdasdasda");
        Glide.with(getActivity()).load(URL.genghuanURL)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                .skipMemoryCache(true)
                .into(tupian);
    }

    private class ImageTouchListener implements View.OnTouchListener {

        //声明一个坐标点
        private PointF startPoint;
        //声明并实例化一个Matrix来控制图片
        private Matrix matrix = new Matrix();
        //声明并实例化当前图片的Matrix
        private Matrix mCurrentMatrix = new Matrix();


        //缩放时初始的距离
        private float startDistance;
        //拖拉的标记
        private static final int DRAG = 1;
        //缩放的标记
        private static final int ZOOM = 2;
        //标识记录
        private int mode;
        //缩放的中间点
        private PointF midPoint;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    //此时处于拖拉方式下
                    mode = DRAG;
                    //获得当前按下点的坐标
                    startPoint = new PointF(event.getX(), event.getY());
                    //把当前图片的Matrix设置为按下图片的Matrix
                    mCurrentMatrix.set(tupian.getImageMatrix());
                    break;
                case MotionEvent.ACTION_MOVE:
                    //根据不同的模式执行相应的缩放或者拖拉操作
                    switch (mode) {
                        case ZOOM:
                            //计算缩放的距离
                            float endDistance = distance(event);
                            //计算缩放比率
                            float scale = endDistance / startDistance;
                            //设置当前的Matrix
                            matrix.set(mCurrentMatrix);
                            //设置缩放的参数
                            matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                            break;
                        default:
                            break;
                    }

                    break;
                //已经有一个手指按住屏幕，再有一个手指按下屏幕就会触发该事件
                case MotionEvent.ACTION_POINTER_DOWN:
                    //此时为缩放模式
                    mode = ZOOM;
                    //计算开始时两个点的距离
                    startDistance = distance(event);
                    //当两个点的距离大于10时才进行缩放操作
                    if (startDistance > 10) {
                        //计算中间点
                        midPoint = mid(event);
                        //得到进行缩放操作之前，照片的绽放倍数
                        mCurrentMatrix.set(tupian.getImageMatrix());
                    }
                    break;
                //已经有一个手指离开屏幕，还有手指在屏幕上时就会触发该事件
                case MotionEvent.ACTION_POINTER_UP:
                    mode = 0;
                    break;
                case MotionEvent.ACTION_UP:
                    mode = 0;
                    break;
                default:
                    break;
            }
            //按照Matrix的要求移动图片到某一个位置
            tupian.setImageMatrix(matrix);
            //返回true表明我们会消费该动作，不需要父控件进行进一步的处理
            return true;
        }


    }

    public static float distance(MotionEvent event) {
        float dx = event.getX(1) - event.getX(0);
        float dy = event.getY(1) - event.getY(0);

        return (float) Math.sqrt(dx * dx + dy * dy);
    }

    public static PointF mid(MotionEvent event) {
        float x = (event.getX(1) - event.getX(0)) / 2;
        float y = (event.getY(1) - event.getY(0)) / 2;
        return new PointF(x, y);
    }
}